from urllib import request
import re
import sys
import random
import os
import json

def check_file_name(name):
    return re.sub(r'"\\|/|:|\*|\?|\"|<|>|\|"', '', name)
def save_img(comic_name, chapter_name, pic_name, pic_url):
    appendix = pic_url.split('.')[-1]
    save_img(comic_name, chapter_name, pic_name, pic_url, appendix)
def save_img(comic_name, chapter_name, pic_name, pic_url, appendix):
    comic_name = check_file_name(comic_name)
    chapter_name = check_file_name(chapter_name)
    folder = sys.path[0] + '\\' + comic_name + '\\' + chapter_name
    file_name = folder + '\\' + pic_name + '.' + appendix
    if os.path.exists(file_name):
        return
    opener = request.build_opener()
    ua_list = ['Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0',
               'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36',
               'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.114 Safari/537.36 Edg/103.0.1264.62',
               'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0',
               'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36 SE 2.X MetaSr 1.0'
               ]
    opener.addheaders = [('User-Agent', random.choice(ua_list))]
    request.install_opener(opener)
    response = request.urlopen(pic_url, timeout=5)
    if not os.path.exists(folder):
        os.makedirs(folder)
    f = open(file_name, 'wb')
    f.write(response.read())
    f.close()


# task is in a format of
# {
#     'comic_name': {
#         "chapter_name": {
#             "pic name without appendix": "pic url"
#         }
#     }
# }

task_file = open('./task.json', 'r', encoding='UTF8')
download_task = dict(json.load(task_file))
task_file.close()
comic_names = list(download_task.keys())
for comic_name in comic_names:
    chapter_names = list(download_task[comic_name].keys())
    for chapter_name in chapter_names:
        if len(download_task[comic_name][chapter_name]) == 0:
            download_task[comic_name].pop(chapter_name)

for comic_name in comic_names:
    if len(download_task[comic_name]) == 0:
        del download_task[comic_name]

task_file = open('./task.json', 'w', encoding='UTF8')
json_data = json.dumps(download_task, ensure_ascii=False)
task_file.write(json_data)
task_file.close()


for comic_name in comic_names:
    chapter_names = download_task[comic_name].keys()
    count = 0
    print('Start task on comic: \t' + comic_name)
    for chapter_name in chapter_names:
        count = count + 1
        failed_tasks = {}
        for image_name in download_task[comic_name][chapter_name].keys():
            image_url = download_task[comic_name][chapter_name][image_name]
            try:
                save_img(comic_name, chapter_name, image_name, image_url, 'jpg')
                print("img: \t" + image_name)
            except Exception as e:
                failed_tasks[image_name] = image_url
                print(e)
                print('Task failed, Comic name:\t' + comic_name + '\tChapter name:\t' + chapter_name + '\tImage info:\t' + image_name + ':' + image_url)
        download_task[comic_name][chapter_name] = failed_tasks
        task_file = open('./task.json', 'w', encoding='UTF8')
        json_data = json.dumps(download_task, ensure_ascii=False)
        task_file.write(json_data)
        task_file.close()
        print('Comic name:\t' + comic_name + '\tChapter process:\t' + str(count) + '/' + str(len(chapter_names)))
